/**
 * 默认值合并
 */
export function mergeDefaults(options: any, defaults: any) {
  if (!options) {
    return defaults;
  }
  for (const key in defaults) {
    if (!(key in options)) {
      options[key] = defaults[key];
    } else if (typeof options[key] === "object") {
      options[key] = mergeDefaults(options[key], defaults[key]);
    }
  }
  return options;
}

// 处理宽度赋值
export const widthLength = (width: number | undefined, minWidth: number | undefined) => {
  if (minWidth && !width) width = minWidth;
  if (!width) return "1fr"; // 剩余宽度平分
  return typeof width === "number" ? `${width}px` : width;
};
export const widthAaverage = (list: any[]) => {
  const total = list.reduce((total, item) => total + (item.width ?? 0), 0);
  const len = list.filter(item => !item.width).length;
  console.log(`calc((100% - ${total}px) / ${len})`);
  return `calc((100% - ${total}px) / ${len})`;
};

/**
 * className
 */

export const classNameExpand = (classVal: any) => {
  const result: TypedObject = {};
  if (typeof classVal === "string") {
    classVal.split(" ").forEach(item => {
      result[item] = true;
    });
  }
  return result;
};

/**
 * 发布订阅类
 */
export class EventBus {
  eventList: any = {};
  constructor() {
    this.eventList = {};
  }
  on(type: string, fn: Function) {
    (this.eventList[type] = this.eventList[type] ?? []).push(fn);
  }
  emit(type: string, ...args: any[]) {
    this.eventList[type]?.forEach((fn: Function) => {
      fn.apply(this, args);
    });
  }
  off(type: string, fn: Function) {
    this.eventList[type] = this.eventList[type]?.filter((item: Function) => item !== fn);
  }
}
/**
 * 找出数组中新增和删除的项
 * @param oldArray
 * @param newArray
 * @param key
 * @returns
 */

export const findChanges = (oldArray: any, newArray: any, key = "id") => {
  const oldSet = new Set(oldArray.map((item: any) => item[key]));
  const newSet = new Set(newArray.map((item: any) => item[key]));

  const addedItems = newArray.filter((item: any) => !oldSet.has(item[key]));
  const removedItems = oldArray.filter((item: any) => !newSet.has(item[key]));
  return { addedItems, removedItems };
};

/**
 * 深拷贝
 */

// 克隆函数
function cloneFunction(func: any) {
  // 使用 toString() 获取函数代码
  const body = func.toString();

  // 判断是否是箭头函数或普通函数
  if (body.startsWith("function") || body.startsWith("(")) {
    // 使用 new Function() 重新创建函数
    return new Function("return " + body)();
  } else {
    // 处理简写方法（如对象方法）
    return eval("(" + body + ")");
  }
}
export const deepClone: any = (obj: any, hash = new WeakMap()) => {
  // 处理基本类型和 null
  if (obj === null || typeof obj !== "object") {
    return obj;
  }

  // 处理函数
  if (typeof obj === "function") {
    return cloneFunction(obj);
  }

  // 处理循环引用
  if (hash.has(obj)) {
    return hash.get(obj);
  }

  // 处理数组
  if (Array.isArray(obj)) {
    const result: any = [];
    hash.set(obj, result);
    for (const item of obj) {
      result.push(deepClone(item, hash));
    }
    return result;
  }

  // 处理对象
  const result: any = {};
  hash.set(obj, result);
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      result[key] = deepClone(obj[key], hash);
    }
  }
  return result;
};
